package com.ytjj.qmyx.admin.controller.supplyChain;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.ProductAttributeReq;
import com.ytjj.qmyx.admin.utils.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 商品属性管理
 */
@RestController
@RequestMapping("/productAttributeNew")
public class ProductAttributeNewController {

    @Resource
    private SupplyApiConfig supplyApiConfig;

    /**
     * 新增编辑
     */
    @PostMapping("/addOrEdit")
    public String addOrEdit(@RequestBody ProductAttributeReq request){
        Admin admin = UserUtil.getAdmin();
        if (null == request.getId()){
            request.setCreater(admin.getCompellation());
            request.setReviser(admin.getCompellation());
        }else {
            request.setReviser(admin.getCompellation());
        }
        String s = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/productAttribute/addOrEdit",
                JSONObject.toJSONString(request), supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 列表
     * @param id  属性id
     * @param attributeName 属性名称
     * @param attributeType 属性类型
     * @param status 状态
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam(value = "id", required = false) Integer id,
                             @RequestParam(value = "attributeName", required = false) String attributeName,
                             @RequestParam(value = "attributeType", required = false) Integer attributeType,
                             @RequestParam(value = "status", required = false) Integer status,
                             @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        Map<String,String> map = new HashMap<>();
        if (null != id) map.put("id",id.toString());
        if (null != status)  map.put("status",status.toString());
        if (null != attributeType) map.put("attributeType",attributeType.toString());
        if (StringUtils.isNotBlank(attributeName)) map.put("attributeName",attributeName);
        map.put("pageNum",pageNum.toString());
        map.put("pageSize",pageSize.toString());
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/productAttribute/list", map, supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 移除
     */
    @GetMapping("/delete")
    public String delete(@RequestParam Integer id){
        Map<String,String> map = new HashMap<>();
        map.put("id",id.toString());
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/productAttribute/delete", map, supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 根据类目获取属性
     * @param topCategoryId
     * @param categoryId
     * @param bottomCategoryId
     * @return
     */
    @GetMapping("/attributeByCategory")
    public String attributeByCategory(@RequestParam(value = "topCategoryId") Integer topCategoryId,
                                      @RequestParam(value = "categoryId", required = false) Integer categoryId,
                                      @RequestParam(value = "bottomCategoryId", required = false) Integer bottomCategoryId,
                                      @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize){
        Map<String,String> map = new HashMap<>();
        map.put("topCategoryId",String.valueOf(topCategoryId));
        map.put("categoryId",categoryId == null? null : String.valueOf(categoryId));
        map.put("bottomCategoryId",bottomCategoryId == null? null : String.valueOf(bottomCategoryId));
        map.put("pageNum", String.valueOf(pageNum));
        map.put("pageSize",String.valueOf(pageSize));
        return HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/productAttribute/attributeByCategory", map, supplyApiConfig.getHeadsMap());
    }
}
